<?php
class CategoriesController extends AppController {

	var $name = 'Categories';
	
	function beforeFilter(){
		if($this->Session->read('Auth.User')){
			$this->Auth->allow('*');
		}
	}

	function admin_index() {
		$this->Category->recursive = 0;
		$this->set('categories', $this->paginate());
	}

	function admin_save(){
		$this->autoRender = false;
		if (!empty($this->data)) {
			$this->Category->create();
			if ($this->Category->save($this->data)) {
				$locations = $this->Category->find('first', array(
														'conditions' => array('Category.id' => $this->Category->id)));
				$this->set('location',$locations);
				$this->Render('row');
			}
		}
	}
	
	function admin_add($id = NULL) {
		if(($this->RequestHandler->isAjax())) {
			if($id != NULL){
				$this->data = $this->Category->read(null, $id);
			}
		}
	}

	function admin_delete($id = null) {
		if(($this->RequestHandler->isAjax())) {
			$this->autoRender = false;
			if (!$id) {
				return 'error';
			}
			if ($this->Category->delete($id)) {
				return 'Success';
			}
			return 'error';
		}
	}
}
?>